package com.example.luke.shengdangxiaoattendance.Util;

import android.app.Activity;

import com.example.luke.shengdangxiaoattendance.Model.MICard;

import java.io.IOException;

/**
 * Created by luke on 1/1/17.
 */

public class ZYWriteTool extends ZYScannerModel {
    private MICard miCard;
    private int currentBalance;//卡当前金额


    public ZYWriteTool(Activity activity){


    }

    public void setMiCard(MICard miCard) {
        this.miCard = miCard;
    }

//    public   void prepareToWrite(){
//
//        try {
//
//            //限度卡后写卡
//            startRead();
//
//
//            if (readCardStatus){
//
//                issueCard();
//
//
//            }
//
//
//
//
//        }catch (Exception e){
//
//           GlobalFunction.printTextByTag("error");
//
//        }
//
//
//
//    }


    public void issueCard() throws IOException {

        MIReaderUtils.MIReaderObj miReaderObj=getCorrespondSectorObj(READ_SECTOR+1);
        miReaderObj.getBlock().set(2,getNameHexStr());
        miReaderObj.getBlock().set(0,agePhoneNumEtc());


        MIReaderUtils.MIReaderObj miReaderObj2= getCorrespondSectorObj(READ_SECTOR);
        miReaderObj2.getBlock().set(2,checkCardNoPart());
        miReaderObj2.getBlock().set(getBackUpIndex(),chargeBalance(GlobalFunction.convertToInt(miCard.currentBalance)));
      //  startWrite();
    }

    private String getNameHexStr()throws IOException {

      MIReaderUtils.MIReaderObj obj=miReaderObjArrayList.get(1) ;
        String name=MIReaderUtils.checkContentLength(MIReaderUtils.chineseToGBK(miCard.name),15);

        return MIReaderUtils.replaceStringAtRange(obj.getBlock().get(2),0,15, name) ;


    }

    private String agePhoneNumEtc(){
        String result;
        String temp=getMiCardValue(READ_SECTOR+1,0);
        temp=MIReaderUtils.replaceStringAtIndex(temp,8,miCard.gender);//插入性别
        temp=MIReaderUtils.replaceStringAtRange(temp,9,15,"0000"+getPhoneHexStr());

        result=temp;

        return result;

    }

    public String getPhoneHexStr(){
        String front=miCard.phone.substring(0,9);
        String behind=miCard.phone.substring(9,11);
        front=MIReaderUtils.convertToHexString(Integer.valueOf(front));
        behind=MIReaderUtils.convertToHexString(Integer.valueOf(behind));

        front=MIReaderUtils.convertHexPosition(front);


        return front+behind;


    }
    private String checkCardNoPart(){
        String result;
        String cardID=MIReaderUtils.convertToHexString(Integer.valueOf(miCard.cardID));
        cardID=MIReaderUtils.checkContentLength(cardID,3);

        cardID=MIReaderUtils.convertHexPosition(cardID);

        String newType=MIReaderUtils.replaceStringAtIndex(getMiCardValue(READ_SECTOR,2),4,miCard.cardType);
        newType=MIReaderUtils.replaceStringAtRange(newType,0,1,cardID.substring(0,4));
        newType=MIReaderUtils.replaceStringAtIndex(newType,14,cardID.substring(4,6));
        result=newType;//已完成插入卡号

        if (miCard.birthDate.length()>0){
            result=MIReaderUtils.replaceStringAtRange(result,6,7,MIReaderUtils.convertToHexString(Integer.valueOf(miCard.birthDate)));

        }

        if (miCard.expiredDate.length()>0){
            result=MIReaderUtils.replaceStringAtRange(result,11,13,MIReaderUtils.convertToHexString(Integer.valueOf(miCard.expiredDate)));
        }




        return result;

    }

   public String chargeBalance(int balance){
        String amount=MIReaderUtils.convertToHexString(balance);
        amount=MIReaderUtils.checkContentLength(amount,3);
        GlobalFunction.printTextByTag("amount:"+amount);

        String temp=getMiCardValue(READ_SECTOR,getBackUpIndex());


        return MIReaderUtils.replaceStringAtRange(temp,4,6,MIReaderUtils.convertHexPosition(amount));


    }



    Integer getBackUpIndex(){
        Integer index=0;
        String value=getMiCardValue(READ_SECTOR,0).substring(0,4);
        String value2=getMiCardValue(READ_SECTOR,1).substring(0,4);

        Integer integer= Integer.valueOf(MIReaderUtils.convertToDecimal(value));
        Integer integer2= Integer.valueOf(MIReaderUtils.convertToDecimal(value2));
        if (integer>integer2){
            index=0;

        }else if(integer2>integer){
            index=1;

        }else {
            value= getMiCardValue(READ_SECTOR, 1).substring(8, 14);

            String temp=MIReaderUtils.convertToDecimal(value);
            Integer integer3= Integer.valueOf(temp);

            String value3=MIReaderUtils.convertToDecimal(getMiCardValue(READ_SECTOR, 0).substring(8, 14)) ;
            Integer integer1= Integer.valueOf(value3);

            if (integer1<integer3){
                index=0;

            }else {
                index=1;
            }
        }

        return index;

    }




}
